package com.xuan.friend.service.user.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xuan.common.core.constants.Constants;
import com.xuan.common.core.domain.PageRequest;
import com.xuan.common.core.domain.TableDataInfo;
import com.xuan.common.core.exception.BusinessException;
import com.xuan.common.core.utils.ThreadLocalUtils;
import com.xuan.friend.domain.message.Message;
import com.xuan.friend.domain.message.vo.MessageTextVo;
import com.xuan.friend.manager.MessageCacheManager;
import com.xuan.friend.mapper.message.MessageTextMapper;
import com.xuan.friend.mapper.message.UserMessageMapper;
import com.xuan.friend.service.user.UserMessageService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
* @author ASUS
* @description 针对表【tb_message(消息表)】的数据库操作Service实现
* @createDate 2024-12-11 20:05:49
*/
@Service
public class UserMessageServiceImpl extends ServiceImpl<UserMessageMapper, Message>
    implements UserMessageService {


    @Resource
    private MessageCacheManager messageCacheManager;

    @Resource
    private UserMessageMapper userMessageMapper;

    @Resource
    private MessageTextMapper messageTextMapper;



    @Override
    public TableDataInfo listContestMessage(PageRequest pageRequest) {
        // 根据 userId 查询每个用户对应的竞赛消息
        Long userId = ThreadLocalUtils.get(Constants.USER_ID, Long.class);
        // 先查询 redis, 再查询数据库
        Long total = messageCacheManager.getMessageListSize(userId);
        List<MessageTextVo> messageTextVoList;
        if (total == null || total <= 0) {
            // redis 中没有缓存数据,从数据库中查询
            PageHelper.startPage(pageRequest.getPageNum(), pageRequest.getPageSize());
            messageTextVoList = userMessageMapper.listUserMessageVo(userId);
            messageCacheManager.refreshUserMessageCache(userId);     // 刷新数据到缓存中
            total = new PageInfo(messageTextVoList).getTotal();
        } else {
            // TODO 如果存在新结束的竞赛,这里可能会有问题
            // 从 redis 中查询对应的数据
            messageTextVoList = messageCacheManager.getMessageTextVoList(pageRequest, userId);
            // 如果出现异常情况, 刷新缓存后 redis 中的数据会发生变化, 重新获取
            total = messageCacheManager.getMessageListSize(userId);
        }
        if (CollectionUtils.isEmpty(messageTextVoList)) {
            return TableDataInfo.empty();
        }
        return TableDataInfo.success(messageTextVoList, total);
    }

    @Override
    @Transactional
    public boolean deleteMessage(Long textId) {
        messageTextMapper.deleteById(textId);
        Long userId = ThreadLocalUtils.get(Constants.USER_ID, Long.class);
        int delete = userMessageMapper.delete(new LambdaQueryWrapper<Message>()
                .eq(Message::getTextId, textId)
                .eq(Message::getRecId, userId));
        if (delete <= 0) {
            throw new BusinessException("删除消息失败,请重试");
        }
        // 删除用户消息缓存(u:m:l:   m:d:)
        messageCacheManager.deleteCache(textId, userId);
        return true;
    }
}




